/**
 * 抵扣设置详情
 */
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { messages } from "share/common";
import { Form, Radio, Icon, Popover } from 'antd'
import SlideFrameContent from '../slide-frame-content'
import settingService from '../request-and-expense-settings.service'

const RadioGroup = Radio.Group;

class DeductionSettingDetail extends Component {

  record = this.props.params.record || {};

  priorityInfo = {
    top: (this.props.authPolicyAll || this.record.deductionOptId) ? 'TENANT' : 'COMPANY',
    disabled: !!this.record.deductionOptId,
    value: this.record.deductionOptId ? {
      levelCode: this.record.levelCode,
      levelOrgId: this.record.levelOrgId,
      levelOrgName: this.record.levelOrgName,
      distribution: {
        companies: this.record.companyInfos
      }
    } : {
      levelCode: this.props.authPolicyAll ? 'TENANT' : 'COMPANY',
      levelOrgId: this.props.authPolicyAll ? this.props.tenant.id : this.props.company.id,
      levelOrgName: this.props.authPolicyAll ? this.props.tenant.tenantName : this.props.company.name,
    }
  };

  popoverContent = (
    <div>
      {messages('expense-4.key1')/*足额报销：若发票金额=100元，关联费用（无其他发票）的费用金额等于100元*/} <br/>
      {messages('expense-4.key2')/*不足额报销：若发票金额=100元，关联费用（无其他发票）的费用金额小于100元*/} <br/>
      {messages('expense-4.key3')/*超额报销：若发票金额=100元，关联费用（无其他发票）的费用金额大于100元（系统默认不允许超额报销。允许超额报销，请至【费控标准-费用管控】页面配置“费用金额与发票价税合计总和校验”为“不校验”），超额报销时，费用原币金额等于发票税额总和*/}
    </div>
  );

  radioOptions1 = [
    {label: messages('expense-4.key4')/*费用金额*/, value: 1},
    {label: messages('expense-4.key5')/*费用原币金额*/, value: 0},
  ];

  radioOptions2 = [
    {label: messages('expense-4.key6')/*全额*/, value: 1},
    {label: messages('expense-4.key7')/*按比例折算*/, value: 0},
  ];

  formList = [{
    id: 'apportionAmountLogic',
    label: messages('expense-4.key8')/*分摊总金额取值逻辑*/,
    extra: (
      <div>
        {messages('expense-4.key9')/*配置费用关联可抵扣的发票时，分摊总金额取值逻辑。*/} <br/>
        {messages('expense-4.key10')/*为“费用金额”，按照费用金额分摊；*/} <br/>
        {messages('expense-4.key11')/*为“费用原币金额”，按照费用原币金额分摊。费用不关联发票以及关联不可抵扣的发票，均按照费用金额分摊*/}
      </div>
    ),
    initialValue: this.record.deductionOptId ? this.record.apportionAmountLogic: 0,
    component: <RadioGroup options={this.radioOptions1}/>

  }, {
    id: 'oriAmountLogic',
    label: (
      <div>
        {messages('expense-4.key12')/*费用原币税额计算逻辑*/}
        <Popover content={this.popoverContent}>
          <Icon type="info-circle-o" style={{ marginLeft: 5 }}/>
        </Popover>
      </div>
    ),
    extra: (
      <div>
        {messages('expense-4.key13')/*配置发票不足额报销时，是否做全额抵扣。*/} <br/>
        {messages('expense-4.key14')/*为“全额”，费用原币税额等于关联发票税额总和；*/} <br/>
        {messages('expense-4.key15')/*为“按比例折算”，费用原币税额=费用金额/关联发票价税合计总和*关联发票税额总和*/}
      </div>
    ),
    initialValue: this.record.deductionOptId ? this.record.oriAmountLogic: 0,
    component: <RadioGroup options={this.radioOptions2}/>
  }];

  dealParams = values => {
    this.record.deductionOptId && (values.deductionOptId = this.record.deductionOptId);
    return values
  };

  render() {
    return (
      <SlideFrameContent
        disabled={this.record.readOnly}
        enabled={this.record.deductionOptId ? this.record.enabled : true}
        priorityInfo={this.priorityInfo}
        formInfo={[
          {title: messages('expense-4.key16')/*抵扣设置*/, list: this.formList}
        ]}
        service={settingService.updateDeductionSetting}
        dealParams={this.dealParams}
        close={this.props.close}
      />
    )
  }

}

function mapStateToProps(state) {
  return {
    authPolicyAll: state.main.authPolicyAll,
    company: state.login.company,
    tenant: state.login.tenant,
  }
}

const WrappedDeductionSettingDetail = Form.create()(DeductionSettingDetail);

export default connect(mapStateToProps)(WrappedDeductionSettingDetail);
